package com.arron.juc;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Classname ProConsumer_BlockingQueueDemo
 * @Description TODO
 * @Date 2020/8/21 21:32
 * @Created by 刘
 */



class ShareResource{

    private volatile boolean FLAG = true;
    private AtomicInteger atomicInteger = new AtomicInteger();
    private BlockingQueue<String> blockingQueue = null;

    public ShareResource(BlockingQueue<String> blockingQueue) {
        this.blockingQueue = blockingQueue;
        //打印出传递的实现类
        System.out.println(blockingQueue.getClass().getName());
    }

    //生产方法
    public void pro() throws InterruptedException {
        String data = null;
        boolean offer;
        while (FLAG){
            data = atomicInteger.incrementAndGet()+"";
            offer = blockingQueue.add(data);
            if (offer){
                System.out.println(Thread.currentThread().getName()+"\t生产蛋糕队列"+data+"成功");
            }else {
                System.out.println(Thread.currentThread().getName()+"\t生产蛋糕队列"+data+"失败");
            }
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("主人叫停止生产蛋糕，已停止生产蛋糕");
    }
    //消费方法
    public void consumer() throws InterruptedException {
        String poll = null;
        while (FLAG){
             poll = blockingQueue.take();
             Thread.sleep(5000);
             if (null == poll || "".equals(poll)){
//                 FLAG = false;
                 System.out.println(Thread.currentThread().getName()+"\t消费蛋糕队列失败");
//                 return;
                 break;
             }else {
                 System.out.println(Thread.currentThread().getName()+"\t消费蛋糕队列"+poll+"成功");
             }
        }
        System.out.println("主人叫停止消费蛋糕，已停止消费蛋糕");
    }


    //停止生产和消费方法
    public void stop(){
        this.FLAG = false;
    }

}


public class ProConsumer_BlockingQueueDemo {

    public static void main(String[] args) {

        ShareResource shareResource = new ShareResource(new ArrayBlockingQueue<>(10));

        CyclicBarrier cyclicBarrier = new CyclicBarrier(2);

        new Thread(()->{
            try {
                System.out.println("生产者线程准备");
                cyclicBarrier.await();
                shareResource.pro();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"AA").start();

        new Thread(()->{
            try {
                System.out.println("消费者线程准备\n\n");
                cyclicBarrier.await();
                shareResource.consumer();
            } catch (Exception e) {
                e.printStackTrace();
            }
        },"BB").start();


        //主线程睡眠5s
        try {
            TimeUnit.SECONDS.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //停止生产
        shareResource.stop();
    }



}
